﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using HRProject.Code.DTO;
using HRProject.Code.Common;
using HRProject.Code.DAO;

namespace HRProject
{
    public partial class AjaxAgent2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sEvent = Request.Params["event"];
            string sFullName = Request.Params["query"];
            if (!"type".Equals(sEvent))
            {
                return;
            }

            AgentDAO agentDao = new AgentDAO();
            BLAgent filter = new BLAgent();
            filter.Id = Guid.Empty;
            filter.FullName = (sFullName != null ? sFullName.Trim() : string.Empty);
            filter.Code = string.Empty;
            filter.IdentifiedSerial = string.Empty;
            List<BLAgent> agents = agentDao.Search(filter, 1, 10, 1);

            string result = "[";
            for (int i = 0; i < agents.Count; i++)
            {
                if (i > 0)
                {
                    result += ",";
                }
                result += "{";
                result += "\"value\":\"" + agents[i].FullName + "\"";
                result += ",\"data\":{";
                result += "\"id\":\"" + agents[i].Id + "\"";
                result += ",\"birthDate\":\"" + agents[i].BirthDate.ToString(Constant.DateFormat) + "\"";
                result += ",\"identifiedSerial\":\"" + agents[i].IdentifiedSerial + "\"";
                result += "}";
                result += "}";
            }
            result += "]";
            result = "{\"query\": \"Li\", \"suggestions\":" + result + "}";
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
        }
    }
}
